<?php
if(!defined('INCP')) { die("GTFO"); }

session_start();

include('config.inc.php');
include('config.modules.inc.php');

/*
 *  Defining site 'root' directory
 */
if($config['misc']['site_path'] == '') {
    $site_path = realpath(dirname(__FILE__) .'./') . '/';
    define ('site_path', $site_path);
}
else {
    define ('site_path', $config['misc']['site_path']);
}

define('site_url',$config['misc']['site_url']);

include(site_path.'inc/page.inc.php');
include(site_path.'inc/userlib.inc.php');
include(site_path.'inc/miscfuncs.inc.php');


/*
 *  Check for database driver exists
 */
if(file_exists(site_path.'/inc/db_drivers/'.$config['db']['driver'].'.inc.php')) {
    include(site_path.'/inc/db_drivers/'.$config['db']['driver'].'.inc.php');
}
else {
    die(site_path.'Database driver not found');
}


/*
 *  Classs autoloader ( aka modules loader )
 */
function __autoload($class_name) {
    $filename = strtolower($class_name) . '.inc.php';
    $file = site_path . 'inc/' . $filename;

    if (file_exists($file) == false) {
            return false;
    }

    include ($file);

}


/*
 *  Creating new database connection
 */
$db = new metadb;
$db->connect($config['db']['host'],$config['db']['user'],$config['db']['pass']);
$db->select_db($config['db']['dbname']);


/*
 *  Creating global registry
 */
$reg = new Registry($db);

/*
 *  User Lib init
 */
$user = new user($reg);

/*
 * Cache controller init
 */
$cache = new Cache(site_path.'cache');
$reg->set('cache',$cache);


$reg->set('theme', $config['misc']['theme']);
include(site_path . 'themes/'. $config['misc']['theme'] .'/theme.php');
//$reg->set('theme_blocks',$theme_blocks);

$template = new Template($config['misc']['theme']);
/*
 *  Define default theme blocks
 */
foreach($theme_blocks as $val) {
    $template->setGlobal($val,'');
}

$reg->set ('template', $template);

$reg->set('config', $config);
$reg->set('user',$user);

$user->autoLogin();

/*
 * Creating actions\pager class instance
 */
$pager = new Pager($reg);
$pager->setPath(site_path.'pages');
$reg->set('pager',$pager);

?>
